/*
 * @Author       : YeRuoyu
 * @Date         : 2023-04-03 19:38:09
 * @LastEditors  : YeRuoyu ruoyu722@qq.com
 * @LastEditTime : 2023-06-02 15:01:06
 * @FilePath     : \listdemo.c
 * @Description  :
 * Copyright (c) 2023 by YeRuoyu ruoyu722@qq.com, All Rights Reserved.
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "list.h"
#include <time.h>

typedef struct person {
    char* name;
    int age;
}Person_t;

void Person_Print(ListItem_t* pxItem)
{
    Person_t* p = (Person_t*)pxItem->pvContainer; // 将 void* 类型转换为 Person* 类型
    printf("Name: %s, Age: %d\n", p->name, p->age);
}

typedef struct book
{
    char title[20];//一个字符串表示的titile 题目 ；
    char author[20];//一个字符串表示的author作者 ；
    float value;//一个浮点型表示的value价格；
}Book_t;

void Book_Print(ListItem_t* pxItem)
{
    Book_t* p = (Book_t*)pxItem->pvContainer; // 将 void* 类型转换为 Person* 类型
    printf("Tile: %s, Author: %s, Value: %f\n", p->title, p->author, p->value);
}

int main(void)
{
    List_t* xListPerson= (List_t*)malloc(sizeof(List_t));
    vListInitialise(xListPerson);

    struct person p[] = {
        {"p1", 10},
        {"p2", 20},
        {"p3", 13},
        {"p4", 41},
        {NULL, 0},
    };

    int i = 0;
    while (p[i].name != NULL)
    {
        /* 创建节点并加入链表 */
        ListItem_t* pxItem = (ListItem_t*)malloc(sizeof(ListItem_t));
        pxItem->pvContainer = (void*)&p[i];
        vListInsertEnd(xListPerson, pxItem);
        i++;
    }
    printf("List length: %ld\n", ulListLength(xListPerson));
	vListForEach(xListPerson, &Person_Print);

	pList_t xListBook = (pList_t)malloc(sizeof(List_t));
    vListInitialise(xListBook);

    struct book book[] = {
        {"Book 1", "a1", 10},
        {"Book 2", "a2", 13.1},
        {"Book 3", "a3", 9.8876},
        {"Book 4", "a4", 18.456},
        {"Book 5", "a5", 1.56},
        {"Book 6", "a6", 0.57697},
    };
    

   for(int j=0;j<6;j++)
    {
        /* 创建节点并加入链表 */
        ListItem_t* pxItem = (ListItem_t*)malloc(sizeof(ListItem_t));
        pxItem->pvContainer = (void*)&book[j];
        vListInsertEnd(xListBook, pxItem);
    }

    printf("List length: %ld\n", ulListLength(xListBook));
	vListForEach(xListBook, &Book_Print);

	uxListRemove(xListBook->pxIndex->pxPrevious->pxPrevious);
	printf("List length: %ld\n", ulListLength(xListBook));
	vListForEach(xListBook, &Book_Print);

	return 0;
}